
//
//  HPStatusPictureView.swift
//  MyWeibo
//
//  Created by Apeng on 16/4/16.
//  Copyright © 2016年 Apengpeng. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

// 设置图片的宽度大小
private let imageMargin: CGFloat = 3
private let pictureCellID = "pictureCellID"
private let MaxWidth = screenWidth - 2 * homePageCellMargin
private let itemWidth = (MaxWidth - 2 * imageMargin) / 3 // 每张图片的宽度

class HPStatusPictureView: UICollectionView {

    // 传入一个图片模型数组
    var imageURLs: [HPStatusPicture]? {
        
        // 在didSet 中帮绑定图片数据
        didSet {
            // 图片数组的长度
            print(imageURLs?.count)
            // 计算配图的大小
            let pictureSize = caculatePictureViewSize()
            
            // "更新"  视图约束大小
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pictureSize)
            }
            
            imageTestLabel.text = "\(imageURLs?.count ?? 0)"
            
//             刷新
            self.reloadData()
        }
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        // 设置流水布局
        let layout = UICollectionViewFlowLayout()
        
        // 设置无效, 因为在 if 中有了判断
//        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        layout.minimumInteritemSpacing = imageMargin
        layout.minimumLineSpacing = imageMargin
        super.init(frame: frame, collectionViewLayout: layout)
        
        // 注册cell
        self.registerClass(HPStatusPictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        // 设置数据源
        self.dataSource = self
        
        backgroundColor = UIColor.greenColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 懒加载测试label
    private lazy var imageTestLabel = UILabel(text: "imageTestLabel", textColor: UIColor.greenColor(), fontSzie: 40)

}

extension HPStatusPictureView {
    
    private func caculatePictureViewSize() -> CGSize {
        let imageCount = imageURLs?.count ?? 0
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        // 单张图片
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
            // 设置size
            let imageSize = CGSize(width: 100, height: 150)
            layout.itemSize = imageSize
            return imageSize
        }
        
        // 设置 itemSize 的大小
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        // 如果是4 张 放在两行, 一个中间间距
        if imageCount == 4 {
            let w = itemWidth * 2 + imageMargin
            return CGSize(width: w, height: w)
        }
        
        // 其他的多张情况
        // 计算有多少行
        let row = CGFloat((imageCount - 1) / 3 + 1)
        let h = row * itemWidth + (row - 1) * imageMargin
        return CGSize(width: MaxWidth, height: h)
    }
}

extension HPStatusPictureView {
    
    private func setupUI() {
        
        addSubview(imageTestLabel)
        
        imageTestLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
    }
}

extension HPStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! HPStatusPictureCell
        
        // 给cell 赋值
        cell.statusPicture = imageURLs![indexPath.item]
        return cell
    }
}


// 自定义cell 的类
class HPStatusPictureCell:UICollectionViewCell {
    
    var statusPicture: HPStatusPicture? {
        didSet {
            let url = NSURL(string: statusPicture?.thumbnail_pic ?? "")
            // 给图片设置值
            pictureView.sd_setImageWithURL(url)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        // 添加图片
        contentView.addSubview(pictureView)
        
        pictureView.snp_makeConstraints { (make) -> Void in
            // 设置 尺寸  或者边距 和 collection
            
//            make.center.equalTo(self.snp_center)
//            make.size.equalTo(self.snp_size)
            make.edges.equalTo(self.snp_edges)
        }
    }
    
    private lazy var pictureView: UIImageView = {
        
        let imgView = UIImageView()
        imgView.contentMode = .ScaleAspectFill
        imgView.clipsToBounds = true
        return imgView
    }()
}
